package membertask

import (
	"context"
	"strings"
	"time"

	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/setting/model"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/internal/utils/errorhandler"
	"mall/service/setting/rpc/types/setting"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendCreateMemberTaskLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendCreateMemberTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendCreateMemberTaskLogic {
	return &BackendCreateMemberTaskLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// MemberTask management
func (l *BackendCreateMemberTaskLogic) BackendCreateMemberTask(in *setting.MemberTaskInfo) (*setting.BaseIDResp, error) {
	var task model.SysMemberTask
	if in.Sort != nil {
		task.Sort = uint16(*in.Sort)
	} else {
		task.Sort = uint16(9999)
	}
	if in.Name != nil {
		task.Name = strings.TrimSpace(*in.Name)

		if len(task.Name) == 0 {
			logx.Errorw("create member task failed, its name is required")
			return nil, errorx.NewInvalidArgumentError("setting.memberTaskNameRequired")
		}

		filterMap := make(map[string]map[string][]any)
		filterMap[model.SysMemberTaskColumns.Name] = map[string][]any{
			"equalTo": []any{task.Name},
		}
		_, count, err := l.svcCtx.MemberTask.GetList(l.ctx, model.M{}, 0, filterMap)
		if err != nil {
			return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
		}

		if count > 0 {
			return nil, errorx.NewAlreadyExistsError("setting.memberTaskAlreadyExists")
		}
	}
	if in.Slug != nil {
		task.Slug = strings.TrimSpace(*in.Slug)

		if len(task.Slug) == 0 {
			logx.Errorw("create member task failed, its slug is required")
			return nil, errorx.NewInvalidArgumentError("setting.memberTaskSlugRequired")
		}

		filterMap := make(map[string]map[string][]any)
		filterMap[model.SysMemberTaskColumns.Slug] = map[string][]any{
			"equalTo": []any{task.Slug},
		}
		_, count, err := l.svcCtx.MemberTask.GetList(l.ctx, model.M{}, 0, filterMap)
		if err != nil {
			return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
		}

		if count > 0 {
			return nil, errorx.NewAlreadyExistsError("setting.memberTaskAlreadyExists")
		}
	}
	if in.Description != nil {
		task.Description = strings.TrimSpace(*in.Description)
	}
	if in.Remark != nil {
		task.Remark = strings.TrimSpace(*in.Remark)
	}
	if in.PicId != nil {
		task.PicID = *in.PicId
	}
	if in.PicUrl != nil {
		task.PicURL = strings.TrimSpace(*in.PicUrl)
	}
	if in.Gems != nil {
		task.Gems = uint(*in.Gems)
	}
	if in.Growth != nil {
		task.Growth = uint(*in.Growth)
	}
	if in.Integrals != nil {
		task.Integrals = uint(*in.Integrals)
	}
	if in.Status != nil {
		task.Status = uint8(*in.Status)
	}
	task.CreatedAt = time.Now()
	task.UpdatedAt = time.Now()
	err := l.svcCtx.MemberTask.Insert(l.ctx, nil, &task)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	return &setting.BaseIDResp{
		Id:  task.ID,
		Msg: i18n.CreateSuccess,
	}, nil
}
